<template>
	<view>
		<!-- 顶部搜索栏 -->
		<u-navbar title="" placeholder :bgColor="'#FFF9F3'" :border="false" autoBack>
			<view class="search-wrap" slot="center">
				<u-search placeholder="搜索笔记" bgColor="#ffffff" :showAction="false" inputAlign="center" searchIcon=" "
					v-model="name" @search="onSearch" />
			</view>
		</u-navbar>

		<!-- 滚动容器 -->
		<view class="scroll-wrap">
			<scroll-view class="scroll-view_H" scroll-y @scrolltolower="searchNotes" :lower-threshold="50">
				<view class="knowledge-container">
					<view class="knowledge-li" v-for="note in noteList" :key="note.id" @click="goNoteDetail(note)">
						<view class="top">
							{{ note.name }}
						</view>
						<view class="bottom shadow">
							更新时间：{{ note.utcCreate || '未知时间' }}
						</view>
					</view>
				</view>

				<!-- 加载中 -->
				<view v-if="loading" class="loading-text">加载中...</view>

				<!-- 没有结果 -->
				<view v-else-if="!loading && noMore && noteList.length === 0" class="no-more-text">
					没有找到相关笔记～
				</view>

				<!-- 初始无数据 -->
				<view v-else-if="!loading && !noMore && noteList.length === 0" class="no-more-text">
					暂无笔记数据～
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import middleApi from '@/api/middle.js'

	export default {
		data() {
			return {
				name: "",
				noteList: [],
				page: 1,
				pageSize: 10,
				loading: false,
				noMore: false,
			};
		},

		onLoad(query) {
			const keyword = query.name || "";
			if (keyword) {
				this.name = decodeURIComponent(keyword);
				this.resetAndSearch();
			}
		},

		methods: {
			
			// 跳转笔记详情页面
			goNoteDetail(item) {
				const paramObj = {
					id: item.id,
					name: item.name,
					type: item.knowledgeBaseType,
				};
				uni.navigateTo({
					url: "/pages/index/noteDetail?params=" + encodeURIComponent(JSON.stringify(paramObj)),
				});
			},
			
			goBack() {
				uni.navigateBack();
			},

			onSearch() {
				this.resetAndSearch();
			},

			resetAndSearch() {
				this.page = 1;
				this.noMore = false;
				this.noteList = [];
				this.searchNotes();
			},

			async searchNotes() {
				if (this.loading || this.noMore) return;

				if (!this.name.trim()) {
					uni.showToast({
						title: "请输入搜索关键词",
						icon: "none",
					});
					return;
				}

				this.loading = true;
				try {
					const params = {
						type: uni.getStorageSync("bindType"),
						pageQueryDTO: {
							currentPage: this.page,
							pageSize: this.pageSize,
						},
						beautifulSelect: 1,
						code: uni.getStorageSync("bindCode"),
						name: this.name,
					};

					const res = await middleApi.queryNotePage(params);

					if (res?.data?.code === 200) {
						const content = res.data.data?.content || [];

						if (this.page === 1) {
							this.noteList = content;
						} else {
							this.noteList = [...this.noteList, ...content];
						}

						if (content.length < this.pageSize) {
							this.noMore = true;
						} else {
							this.page += 1;
						}
					} else {
						if (this.page === 1) this.noteList = [];
						this.noMore = true;
						uni.showToast({
							title: "未查询到笔记",
							icon: "none",
						});
					}
				} catch (err) {
					console.error("查询笔记异常：", err);
					uni.showToast({
						title: "网络异常",
						icon: "none",
					});
				} finally {
					this.loading = false;
				}
			},
		},
	};
</script>

<style scoped>
	.scroll-wrap {
		width: 100%;
		height: calc(100vh - 88rpx - 80rpx);
		background: white;
		padding-top: 16rpx;
	}

	.scroll-view_H {
		height: 100%;
	}

	.knowledge-container {
		padding: 0 24rpx;
	}

	.knowledge-li {
		width: calc(100% - 48rpx);
		margin: 0 auto 20rpx;
		background: #fff;
		border-radius: 20rpx;
		overflow: hidden;
		box-shadow: 0 6rpx 16rpx rgba(0, 0, 0, 0.04);
		transition: all 0.2s;
		cursor: pointer;
	}

	.knowledge-li:active {
		transform: scale(0.98);
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
	}

	.top {
		padding: 24rpx 32rpx;
		font-weight: 600;
		font-size: 32rpx;
		color: #111;
		background: #FFF9F3;
	}

	.bottom {
		height: 70rpx;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		padding: 0 32rpx;
		font-size: 28rpx;
		color: #666;
		box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.05);
		background: #fafafa;
		border-radius: 0 0 20rpx 20rpx;
	}

	.loading-text,
	.no-more-text {
		width: 100%;
		text-align: center;
		font-size: 28rpx;
		color: #999;
		padding: 40rpx 0;
	}
</style>